<%@ page language="java"
import="java.util.*"
import="javax.imageio.*"
import="java.util.*"
import="javax.xml.*"
import="javax.xml.parsers.*"
import="org.w3c.dom.*"
import="javax.xml.transform.*"
import="javax.xml.transform.dom.*"
import="javax.xml.transform.stream.*" 
import="java.io.StringReader" 
import="java.lang.Integer"
import="org.xml.sax.InputSource"
import="webxml.xmlcmd"
import="webxml.Base64"
import="java.net.URLEncoder"
pageEncoding="UTF-8"
contentType="text/xml;charset=utf-8"
%><%

	String UserName = "";
	String PassWd = "";
	String sendkey = "";
	String ValidateCode = "";
	 // 
	try {
		UserName = request.getParameter("username").toString();
		PassWd	 = request.getParameter("passwd").toString();
		
		if ((UserName.length() ==0) ||(PassWd.length() == 0 )) {
			throw new Exception();
		}
		sendkey = request.getParameter("ValidateCode").toLowerCase();
		ValidateCode = (String) session.getAttribute("ValidateCode");//).toLowerCase();
		
		if (!ValidateCode.equals(sendkey)) {
			out.print("!ValidateCode.equals(sendkey)");
			throw new Exception();
		}
		
		ServletContext Application = getServletContext();		
		Object value = Application.getAttribute("ClientCmd_Array");
		if (value == null) {
			out.print("value == null");
			throw new Exception();
		}
			
		synchronized (Application) {		
			
			xmlcmd []lpxmds = (xmlcmd [])value;
			Date DateNow = new Date();
			boolean findchannel = false;
			int xid = 0;
			//String xxxxxx ="";
			for ( int x = 0 ; x < xmlcmd.clientsize ; x++) {
				
				if ((lpxmds[x].inuseing == false))
				// || 
				//			(DateNow.getTime() >( lpxmds[x].lastfreshtime).getTime()  + xmlcmd.freshmaxtime*1000)) // 如果找到了没有使用过的channel 
						{
							//if (lpxmds[x].inuseing == true) {
								//long tempx = DateNow.getTime();
								//xxxxxx = String.valueOf(tempx);
								//xxxxxx += "    ";
								//tempx = ( lpxmds[x].lastfreshtime).getTime();
								//xxxxxx += String.valueOf(tempx);  	  		
							//}					
							findchannel = true;
							lpxmds[x].inuseing = true;
							
							xid = x;
							// 删除所有的 xmlcmd
							int y = lpxmds[x].cmdsize;						
							for (int j = 0; j < y ;j++) 
							{
								lpxmds[x].onexmlcmd[j] = "";
							}
							lpxmds[x].cmdsize =0;
							
							
							xmlcmd[] Servervalue = (xmlcmd[]) Application.getAttribute("ServerCmd_Array");
							if (Servervalue == null) {
								throw new Exception();
							}
					 
							for (int i = 0; i < Servervalue[x].cmdsize; i++) {
								Servervalue[x].onexmlcmd[i]= "";						 
							}
							Servervalue[x].cmdsize = 0;
							break;
						} 
			}
			
			if (findchannel	) {
			
						Random  xrandom = new Random(); 
						Integer xtempNumber = new Integer((xrandom.nextInt(9999)+1000)%9999); 
						String RadId = xtempNumber.toString();
						// "1111";//request.getParameter("rand"); 
						
						//Session["keyauth"]			= RadId.ToString();
						//Session["UserFreshTime"]	= DateNow;
						//Session["UserName"]			= UserName;
						//Session["deskey"]			= deskey;
						//Session["iv"]				= iv;
						//Session["logined"]			= true;
						//Session["channelid"]		= xid;
						//Session["UserId"]			= xid;
						
						session.setAttribute("keyauth",RadId);						
						//Session[""]			= ;
						session.setAttribute("UserFreshTime",DateNow);	
						//Session[""]	= DateNow;
						
						session.setAttribute("UserName", UserName);
						//Session["UserName"]			= UserName;
						// 暂时还不支持
						//Session["deskey"]			= deskey;
						//Session["iv"]				= iv;
						Boolean b =  new Boolean (true);
						session.setAttribute("logined", b);
						//Session["logined"]			= true;
						Integer bb =  new Integer(xid);
						session.setAttribute("channelid", bb);
						//Session["channelid"]		= xid;
						Integer cc =  new Integer(xid);
						session.setAttribute("UserId", cc);
						//Session["UserId"]			= xid;
						
	
						
						lpxmds[xid].userkey			= xtempNumber.intValue();    // 设置idkey
						lpxmds[xid].name			= UserName;	// 用户名
						lpxmds[xid].lastfreshtime	= DateNow;
						
						
						String XmlFile = "";
					 
						
						XmlFile = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
						XmlFile += "<root><xmlcmd>";						
						XmlFile +="<keyauth>" + RadId.toString() + "</keyauth>";
						XmlFile +="<username>"+ UserName+ "</username>";
						XmlFile +="<receive>"+ "MSNSERVER"+ "</receive>";
						XmlFile +="<currtime>"+ DateNow.toString()+ "</currtime>";
						Integer tempxid = new  Integer(xid);
						XmlFile +="<channelid>"+ tempxid.toString()+ "</channelid>";
						XmlFile +="<msg>";
						XmlFile +="<user>";
						//URLEncoder ma;
						XmlFile += URLEncoder.encode(UserName, "UTF-8");
						XmlFile +="</user><pass>";
						XmlFile += URLEncoder.encode(PassWd, "UTF-8");
						XmlFile += "</pass>";
						XmlFile +=	 "</msg>";
						XmlFile += "<codes>1060</codes>";	
						XmlFile += "</xmlcmd></root>";
						
						lpxmds[xid].onexmlcmd[lpxmds[xid].cmdsize] = XmlFile;
						lpxmds[xid].cmdsize++;
						
						XmlFile = "";
						XmlFile = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
						XmlFile += "<root><xmlcmd>";
						XmlFile += "<userid>11</userid>";
						XmlFile += "<username >" + UserName + "</username>";
						XmlFile += "<codes>2060</codes>";
						XmlFile += "</xmlcmd></root>";
						session.setMaxInactiveInterval( 60 * 10);
						out.print(Base64.encode(XmlFile));
				} else {
				
					out.print("unfind");
				}
		}	// end of  synchronized (Application)

	} catch (Exception e) {
		//out.print("Exception e!");
		//out.print(e.getMessage());	
		String XmlFile = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
		XmlFile += "<root><xmlcmd>";
		XmlFile += "<userid>11</userid>";
		XmlFile += "<username >" + UserName + "</username>";
		XmlFile += "<codes>2061</codes>";
		XmlFile += "</xmlcmd></root>";
		out.print(Base64.encode(XmlFile));
	}
	%>
